package com.ll.demo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class SmallChangeSysOOP {
    public static void main(String[] args) {
        new Detail().showMenu();
    }
}

class Detail {
    Scanner scanner = new Scanner(System.in);

    // 格式化时间
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    //存储用户输入
    String key = "";
    //是否退出switch字段的变量
    boolean loop = true;

    //完成收益入账
    double money = 0;
    double balance = 0;

    //消费
    //定义变量,保存消费的原因
    String note = "";

    //使用Stirng拼接
    String details = "---------------零钱通明细如下:-------------\n";

    //显示菜单
    public void showMenu() {
        do {
            // 显示菜单
            System.out.println("===============零钱通菜单==========");
            System.out.println("1 零钱通明细");
            System.out.println("2 收益入账");
            System.out.println("3 消费");
            System.out.println("4 退     出");

            //提示用户属于
            System.out.println("请选择(1-4)");
            key = scanner.next();

            //使用switch分支控制
            switch (key) {
                case "1":
                    this.showDetails();
                    break;
                case "2":
                    this.income();
                    break;
                case "3":
                    this.expenditure();
                    break;
                case "4":
                    this.exit();
                    break;
                default:
                    System.out.println("选择有误 请重新选择");
                    break;
            }
        } while (loop);

        System.out.println("------退出了零钱通项目------");
    }

    // 显示明细
    public void showDetails() {
        System.out.println(details);
    }

    // 收入
    public void income() {
        System.out.print("收益入账金额: ");
        money = scanner.nextDouble();
        //money 的值范围应该校验
        if (money <= 0) {
            System.out.println("收益入账金额需要大于0");
            return;
        }
        System.out.print("收益说明: ");
        note = scanner.next();
        balance += money;
        //拼接收益入账信息到 details
        details += note + "\t+" + money + "  \t" + dateTimeFormatter.format(LocalDateTime.now())
                + "\t余额: " + balance + "\n";
        System.out.println(details);
    }

    /**
     * 消费
     */
    public void expenditure() {
        System.out.print("消费金额: ");
        money = scanner.nextDouble();
        if (money <= 0) {
            System.out.println("错误的消费,消费金额应该大于0");
            return;
        } else if (money > balance) {
            System.out.println("错误的消费,消费金额大余额");
            return;
        }
        System.out.print("消费说明: ");
        note = scanner.next();
        balance -= money;
        details += note + "\t-" + money + "  \t" + dateTimeFormatter.format(LocalDateTime.now()) +
                "\t余额: " + balance + "\n";
        System.out.println(details);
    }

    /**
     * 退出
     */
    public void exit() {
        String choice = "";
        while (true) {
            System.out.println("你确定要退出吗? y/n");
            choice = scanner.next();
            if ("y".equals(choice) || "n".equals(choice)) {
                break;
            }
        }

        // 当用户输入的是y,退出switch语句和while语句
        if ("y".equals(choice)) {
            loop = false;
        }
    }
}
